package com.passion.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.passion.bean.OfflineAnalyseResult;
import com.passion.service.ExportExcelService;
import com.passion.service.OfflineAnalyseResultService;
import com.passion.utils.DateTimeUtil;
import com.passion.utils.UUIDUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @program offline-e-commerce-data-warehouse
 * @Description
 * @Author limeihua
 * @Date 2024/10/07 16:21
 * @Version 1.0
 */
@Service
public class ExportExcelServiceImpl implements ExportExcelService {

    @Autowired
    private OfflineAnalyseResultService offlineAnalyseResultService;

    @Override
    public Workbook JsonArrayToExcel(String sqlID, String params) {
        // 获取任务ID
        Map<String, String> map = new LinkedHashMap<>();
        map.put("sqlID", sqlID);
        String[] split = params.split(",");
        for (String s : split) {
            String[] split1 = s.split(":");
            map.put(split1[0], split1[1]);
        }
        String taskID = UUIDUtils.generateUUID(map);
        LambdaQueryWrapper<OfflineAnalyseResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OfflineAnalyseResult::getSqlId, sqlID).eq(OfflineAnalyseResult::getTaskId, taskID);
        OfflineAnalyseResult result = offlineAnalyseResultService.getOne(queryWrapper);
        String analyseResult = result.getAnalyseResult();
        JSONArray jsonArray = JSONArray.parseArray(analyseResult);
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("sheet1");
        if (!jsonArray.isEmpty()) {
            JSONObject firstObject = jsonArray.getJSONObject(0);
            Set<String> keys = firstObject.keySet();
            // 行首设置单元格格式
            byte[] rgb = new byte[3];
            rgb[0] = (byte) 52;
            rgb[1] = (byte) 156;
            rgb[2] = (byte) 73;
            XSSFColor heardColor = new XSSFColor(rgb, null);
            CellStyle heardCellStyle = workbook.createCellStyle();
            heardCellStyle.setAlignment(HorizontalAlignment.CENTER);
            heardCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            heardCellStyle.setFillForegroundColor(heardColor);
            heardCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            Font font = workbook.createFont();
            font.setFontHeightInPoints((short) 14);
            font.setColor(IndexedColors.WHITE.getIndex());
            heardCellStyle.setFont(font);
            BorderStyle borderStyle = BorderStyle.THIN;
            heardCellStyle.setBorderTop(borderStyle);
            heardCellStyle.setBorderBottom(borderStyle);
            heardCellStyle.setBorderLeft(borderStyle);
            heardCellStyle.setBorderRight(borderStyle);
            // 设置内容单元格式
            byte[] rgbHeard = new byte[3];
            rgb[0] = (byte) 240;
            rgb[1] = (byte) 247;
            rgb[2] = (byte) 241;
            XSSFColor contentColor = new XSSFColor(rgbHeard, null);
            CellStyle contentCellStyle = workbook.createCellStyle();
            contentCellStyle.setAlignment(HorizontalAlignment.CENTER);
            contentCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            contentCellStyle.setFillForegroundColor(contentColor);
            Font font1 = workbook.createFont();
            font.setFontHeightInPoints((short) 14);
            contentCellStyle.setFont(font1);
            contentCellStyle.setBorderTop(borderStyle);
            contentCellStyle.setBorderBottom(borderStyle);
            contentCellStyle.setBorderLeft(borderStyle);
            contentCellStyle.setBorderRight(borderStyle);
            Row headerRow = sheet.createRow(0);
            headerRow.setHeight((short) 1000);
            int columnIndex = 0;
            for (String key : keys) {
                sheet.setColumnWidth(columnIndex, 20 * 256);
                Cell headerCell = headerRow.createCell(columnIndex);
                headerCell.setCellValue(key);
                headerCell.setCellStyle(heardCellStyle);
                columnIndex++;
            }
            int rowIndex = 1;
            for (Object o : jsonArray) {
                JSONObject jsonObject = (JSONObject) o;
                Row dataRow = sheet.createRow(rowIndex++);
                dataRow.setHeight((short) 1000);
                columnIndex = 0;
                for (String key : keys) {
                    Cell dataCell = dataRow.createCell(columnIndex++);
                    dataCell.setCellStyle(contentCellStyle);
                    Object value = jsonObject.get(key);
                    dataCell.setCellValue(value.toString());
                }
            }
        }
        return workbook;
    }
}
